package com.itshizhan.itjidancideveloper.controller.webfront;

import com.itshizhan.itjidancideveloper.dao.WordRootDao;
import com.itshizhan.itjidancideveloper.models.WordRoot;
import com.itshizhan.itjidancideveloper.response.CodeMsgEnum;
import com.itshizhan.itjidancideveloper.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = {"WebFront词根相关接口"})
@RestController()
@RequestMapping("/webfront/root")
public class WordRootFrontController {

	@Autowired
	WordRootDao wordRootDao;

	@ApiOperation("根据关键字，查询相关词根")
	@GetMapping("/query")
	public Response<List<WordRoot>> getByKeyword(@RequestParam("key") String keyword){
		if (keyword.length() < 2) {
			return Response.fail(CodeMsgEnum.ERROR_PARAMETER);
		}
		return Response.success(wordRootDao.findByRootNameLike("%" + keyword +"%"));
	}

	@ApiOperation("根据词根Id，查询词根详情")
	@GetMapping("/detail")
	public Response<WordRoot> getDetailById(@RequestParam("id") Integer id){
		WordRoot wordRoot = wordRootDao.findById(id).orElse(null);
		return Response.success(wordRoot);
	}

}
